package cn.edu.hbcit.text1;

import java.sql.*;

public class Account {
    /**
     * 插入账号
     *
     * @param acount
     * @param password
     * @param name
     * @return flag
     */
    public boolean addAcount(String acount, String password, String name) {
        boolean flag = false;
        Connection conn = DataBaseConnect.getCoon();
        String sql = "INSERT INTO account VALUE (?, ?, ?);";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, acount);
            ps.setString(2, password);
            ps.setString(3, name);
            long num = ps.executeLargeUpdate();
            System.out.println(num);
            if (num > 0) {
                System.out.println("账号插入成功");
                flag = true;
            } else {
                System.out.println("账号插入失败");
                flag = false;
            }
            conn.close();
        } catch (SQLException e) {
            System.out.println("数据库插入异常");
        }
        return flag;
    }

    /**
     * 登陆账号，查找用户
     *
     * @param acount
     * @param password
     * @return flag
     * @throws SQLException
     */
    public boolean findAcount(String acount, String password) {
        boolean flag = false;
        Connection conn = DataBaseConnect.getCoon();
        try {
            String sql = "SELECT * FROM account WHERE number LIKE ? AND password LIKE ? ;";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, acount);
            ps.setString(2, password);
            ResultSet rs = ps.executeQuery();
            if (!rs.next()) {
                flag = false;
            } else {
                flag = true;
            }
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }
}



